package cn.piaoyi.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.piaoyi.bean.Admin;
import cn.piaoyi.bean.Category;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.comm.Constant;
import cn.piaoyi.comm.PiaoyiUtil;
import cn.piaoyi.comm.Util;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.AdminService;
import cn.piaoyi.service.CategoryService;
import cn.piaoyi.service.GoodsService;
import cn.piaoyi.service.web.PageList;
import cn.piaoyi.service.web.Pages;
import cn.piaoyi.web.servlet.AdminSession;
import cn.piaoyi.web.ui.OptionsInt;
import cn.piaoyi.web.ui.OptionsLong;
import cn.piaoyi.web.ui.OptionsString;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

public class AdminGoods extends BaseAction{
	private static final Log logger = LogFactory.getLog(AdminGoods.class);
	
	private GoodsService goodsService;
	private CategoryService categoryService;

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
	
	private List categoryList = new ArrayList();
	private List isShowList = new ArrayList();
	private List stateList = new ArrayList();
	private List cityList = new ArrayList();

	public List getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List categoryList) {
		this.categoryList = categoryList;
	}
	
	public List getIsShowList() {
		return isShowList;
	}

	public void setIsShowList(List isShowList) {
		this.isShowList = isShowList;
	}

	public List getStateList() {
		return stateList;
	}

	public void setStateList(List stateList) {
		this.stateList = stateList;
	}

	public List getCityList() {
		return cityList;
	}

	public void setCityList(List cityList) {
		this.cityList = cityList;
	}

	private void setCategoryListValues(){
		List l = this.getCategoryService().findCategorys("orders", 0);
		categoryList.add(new OptionsLong(-1, "请选择"));
		for(int i=0;i<l.size();i++){
			Category c = (Category) l.get(i);
			categoryList.add(new OptionsLong(c.getId(), c.getName()));
		}
	}
	private void setCategoryCodeListValues(){
		List l = this.getCategoryService().findCategorys("orders", 0);
		categoryList.add(new OptionsString("", "请选择"));
		for(int i=0;i<l.size();i++){
			Category c = (Category) l.get(i);
			categoryList.add(new OptionsString(c.getCode(), c.getName()));
		}
	}
	
	private void setIsShowListValues(){
		isShowList = Constant.GOODS_IS_SHOW;
	}
	private void setStateListValues(){
		stateList = Constant.GOODS_STATE;
	}
	
	private void setCityListValues(){
		cityList = Constant.GOODS_CITY;
	}
	
	private void initValues(){
		setCategoryListValues();
		setIsShowListValues();
		setStateListValues();
		setCityListValues();
	}
	
	private void listInitValues(){
		setCategoryCodeListValues();
		setIsShowListValues();
		setStateListValues();
		setCityListValues();
	}


	private long id;			//主键ID
	private long categoryId = -1;	//类别ID
	private String category = "";	//类别代码
	private String name = "";		//名称
	private String details;		//详情
	private String bigImg;		//大图
	private String smallImg;	//小图
	private String seatImg;		//坐位图
	private Date startTime;		//开始时间
	private Date endTime;		//结束时间
	private int isShow = -1;	//显示
	private int state = -1;		//状态
	private int orders;			//排序
	private int cityId = 1;		//城市
	private String city;		//城市
	private String site;		//场馆

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public String getBigImg() {
		return bigImg;
	}

	public void setBigImg(String bigImg) {
		this.bigImg = bigImg;
	}

	public String getSmallImg() {
		return smallImg;
	}

	public void setSmallImg(String smallImg) {
		this.smallImg = smallImg;
	}

	public String getSeatImg() {
		return seatImg;
	}

	public void setSeatImg(String seatImg) {
		this.seatImg = seatImg;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	public int getIsShow() {
		return isShow;
	}

	public void setIsShow(int isShow) {
		this.isShow = isShow;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}
	
	public int getOrders() {
		return orders;
	}

	public void setOrders(int orders) {
		this.orders = orders;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}	
	
	
	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		listInitValues();
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/admin_goods.action?name=" + this.name + "&category=" + this.category + "&isShow=" + this.isShow + "&state=" + this.state);
		this.setPageList(this.getGoodsService().findGoodses(-1,this.category,this.name,this.isShow,this.state,"id", 1, pages));
		return SUCCESS;
	}
	
	public String add(){
		initValues();
		this.setAction("doadd");
		return INPUT;
	}
	
	public String doadd(){
		//添加商品
		Goods g = new Goods();
		g.setCategoryId(this.categoryId);
		g.setName(this.name);
		g.setDetails(this.details);
		g.setBigImg(this.bigImg);
		g.setSmallImg(this.smallImg);
		g.setSeatImg(this.seatImg);
		g.setStartTime(this.startTime);
		g.setEndTime(this.endTime);
		g.setIsShow(this.isShow);
		g.setState(this.state);
		g.setOrders(this.orders);
		g.setCityId(this.cityId);
		g.setCity(PiaoyiUtil.getCity(this.cityId));
		g.setSite(this.site);
		g.setAddTime(new Date());
		try {
			this.getGoodsService().createGoods(g);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.createrror"));
			return ERROR;
		}
		return "index";
	}
	
	public String update(){
		initValues();
		this.setAction("doupdate");
		Goods g = this.getGoodsService().findGoodsById(this.id);
		this.setCategoryId(g.getCategoryId());
		this.setName(g.getName());
		this.setDetails(g.getDetails());
		this.setBigImg(g.getBigImg());
		this.setSmallImg(g.getSmallImg());
		this.setSeatImg(g.getSeatImg());
		this.setStartTime(g.getStartTime());
		this.setEndTime(g.getEndTime());
		this.setIsShow(g.getIsShow());
		this.setState(g.getState());
		this.setOrders(g.getOrders());
		this.setCityId(g.getCityId());
		this.setCity(g.getCity());
		this.setSite(g.getSite());
		return INPUT;
	}
	
	public String doupdate(){
		//添加商品
		Goods g = this.getGoodsService().findGoodsById(this.id);
		g.setCategoryId(this.categoryId);
		g.setName(this.name);
		g.setDetails(this.details);
		g.setBigImg(this.bigImg);
		g.setSmallImg(this.smallImg);
		g.setSeatImg(this.seatImg);
		g.setStartTime(this.startTime);
		g.setEndTime(this.endTime);
		g.setIsShow(this.isShow);
		g.setState(this.state);
		g.setOrders(this.orders);
		g.setCityId(this.cityId);
		g.setCity(PiaoyiUtil.getCity(this.cityId));
		g.setSite(this.site);
		try {
			this.getGoodsService().updateGoods(g);
		} catch (PiaoyiException e) {
			this.addActionError(this.getText("error.saveerror"));
			return ERROR;
		}
		return "index";
	}
	
	public String del(){
		this.getGoodsService().removeGoodsById(this.id);
		return "index";
	}


	
}
